package com.bank.frame;

import com.bank.dao.AccountDAO;
import com.bank.dao.UserDAO;
import com.bank.pojo.User;
import com.bank.utils.SystemConstants;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;

public class AccountEditPanel extends JInternalFrame {

    public AccountEditPanel(Object selectedId) {
        super("编辑账户", true, true, true, true);
        this.setSize(SystemConstants.FRAME_WIDTH - 20, SystemConstants.FRAME_HEIGHT - 50);
        // 创建内容面板
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        // 设置内部窗口的内容面板
        this.setContentPane(panel);
        Box boxBase = Box.createHorizontalBox();
        Box boxLeft = Box.createVerticalBox();

        //左边的部分将要编辑的字段一次添加进来
        boxLeft.add(new JLabel("卡号"));
        boxLeft.add(Box.createVerticalStrut(8));
        boxLeft.add(new JLabel("身份证号"));
        boxLeft.add(Box.createVerticalStrut(8));
        boxLeft.add(new JLabel("姓名"));
        boxLeft.add(Box.createVerticalStrut(8));
        boxLeft.add(new JLabel("手机号"));
        boxLeft.add(Box.createVerticalStrut(8));
        boxLeft.add(new JLabel("账户类型"));
        boxLeft.add(Box.createVerticalStrut(30));

        //右边的部分是文本框
        Box boxRight = Box.createVerticalBox();
        JTextField field0 = new JTextField(10);
        boxRight.add(field0);
        JTextField field1 = new JTextField(10);
        boxRight.add(field1);
        boxRight.add(Box.createVerticalStrut(5));
        JTextField field2 = new JTextField(10);
        boxRight.add(field2);
        boxRight.add(Box.createVerticalStrut(5));
        JTextField field3 = new JTextField(10);
        boxRight.add(field3);
        boxRight.add(Box.createVerticalStrut(5));

        // -------权限按钮开始--------
        Box radioBox = Box.createHorizontalBox();
        JRadioButton radio1 = new JRadioButton("Ⅰ类", true);// 只传了两个参数
        JRadioButton radio2 = new JRadioButton("Ⅱ类");
        ButtonGroup group = new ButtonGroup();// 创建一个按钮组
        group.add(radio1);
        group.add(radio2);
        radioBox.add(radio1);
        radioBox.add(radio2);
        boxRight.add(radioBox);

        //  接收传来的参数，如果是null就是开户，如果不是null就是修改账户信息
        String card = (String) selectedId;

        // 修改时回填数据
        if (card != null) {
            Object[] data = AccountDAO.findByCard(card);
            //将卡号、身份证号、姓名改为不可更改状态
            field0.setText(data[0].toString());
            field0.setEditable(false);
            field1.setText(data[1].toString());
            field1.setEditable(false);
            field2.setText(data[2].toString());
            field2.setEditable(false);
            field3.setText(data[3].toString());
            //回填单选按钮
            if (SystemConstants.ACCOUNT_TYPE_I.equals(data[6])) {
                radio1.setSelected(true);
            } else {
                radio2.setSelected(true);
            }
        }

        JButton btn = new JButton("提交");
        //点击提交后需要判断输入的数据是否为null
        btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (field0.getText() == null || field0.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(btn.getParent(), "账号不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (field1.getText() == null || field1.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(btn.getParent(), "身份证不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (field2.getText() == null || field2.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(btn.getParent(), "姓名不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (field3.getText() == null || field3.getText().trim().isEmpty()) {
                    JOptionPane.showMessageDialog(btn.getParent(), "手机号不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                User user = UserDAO.getUser(field1.getText());//根据身份证号获取用户信息
                // 这个身份证之前开过户，但是姓名和这次输入的不一样，系统报错
                if (user != null && !user.getRealName().equals(field2.getText())) {
                    JOptionPane.showMessageDialog(btn.getParent(), "姓名输入错误", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 0"账户", 1"身份证", 2"姓名", 3"手机号", 4"余额", 5"开户时间", 6"类型", 7"状态",
                // 8"密码"
                Object[] data = new Object[]{field0.getText(), field1.getText(), field2.getText(), field3.getText(),
                        0.0, SystemConstants.DATE_FORMAT.format(new Date()),
                        radio1.isSelected() ? SystemConstants.ACCOUNT_TYPE_I : SystemConstants.ACCOUNT_TYPE_II,
                        SystemConstants.ACCOUNT_STATUS_AVALIABLE, "000000"};

                //为null就是开户，不为null就是修改
                if (card != null) {
                    Object[] temp = AccountDAO.findByCard(field0.getText());
                    // 余额不能改
                    data[4] = temp[4];
                    // 检查这次是不是把Ⅱ类卡升级成Ⅰ类卡
                    // 检查这次是不是把Ⅱ类卡升级成Ⅰ类卡
                    if (temp[6].equals(SystemConstants.ACCOUNT_TYPE_II) && radio1.isSelected()) {
                        int count = AccountDAO.getUserAccountCount(field1.getText(), SystemConstants.ACCOUNT_TYPE_I);
                        if (count > 0) {
                            JOptionPane.showMessageDialog(btn.getParent(), "用户名下只能有一张Ⅰ类卡", "系统提示",
                                    JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                    }
                    AccountDAO.update(card, data);
                } else {
                    //  开户
                    //  判断用户有几张卡
                    int count = AccountDAO.getUserAccountCount(field1.getText(), null);
                    if (count >= 4) {
                        //  每个用户最多办理4张卡
                        JOptionPane.showMessageDialog(btn.getParent(), "用户名下最多有4张卡", "系统提示",
                                JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    if (radio1.isSelected()) {
                        //  Ⅰ类卡最多一张
                        count = AccountDAO.getUserAccountCount(field1.getText(), SystemConstants.ACCOUNT_TYPE_I);
                        if (count > 0) {
                            JOptionPane.showMessageDialog(btn.getParent(), "用户名下只能有一张Ⅰ类卡", "系统提示",
                                    JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                    }
                    if (AccountDAO.findByCard(field0.getText()) != null) {
                        //  判断办理的卡号是否已经存在
                        JOptionPane.showMessageDialog(btn.getParent(), "卡号已存在", "系统提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    if (user == null) {
                        // 如果在前面根据身份证号获取用户信息时为null，证明无用户，需要创建新用户创建登录用户
                        UserDAO.add(field1.getText(), field2.getText());//【1】是用户身份证号，【2】是姓名
                    }
                    AccountDAO.add(data);  //  添加账号
                }
                com.bank.frame.AdminPanel.setContent(new com.bank.frame.AdminAccountTablePanel());
            }
        });
        boxRight.add(btn);
        boxBase.add(boxLeft);
        boxBase.add(Box.createHorizontalStrut(8));
        boxBase.add(boxRight);
        panel.add(boxBase);
        // 显示内部窗口
        this.setVisible(true);
    }
}
